<template>
  <view class="password-modal" v-if="visible">
    <view class="modal-overlay" @click="handleCancel"></view>
    <view class="modal-content">
      <view class="modal-header">
        <view class="modal-title">{{ title }}</view>
      </view>
      <view class="modal-body">
        <view class="password-label">{{ label }}</view>
        <view class="password-input-container">
          <input 
            type="password" 
            v-model="password" 
            placeholder="请输入密码" 
            class="password-input"
            @keyup.enter="handleConfirm"
          />
        </view>
      </view>
      <view class="modal-footer">
        <button class="cancel-btn" @click="handleCancel">取消</button>
        <button class="confirm-btn" @click="handleConfirm">确认</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'PasswordModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '输入密码'
    },
    label: {
      type: String,
      default: '请输入会员密码'
    }
  },
  data() {
    return {
      password: ''
    }
  },
  methods: {
    handleConfirm() {
      if (this.password.trim()) {
        this.$emit('confirm', this.password);
        this.password = '';
      } else {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        });
      }
    },
    handleCancel() {
      this.$emit('cancel');
      this.password = '';
    }
  },
  watch: {
    visible(newVal) {
      if (!newVal) {
        this.password = '';
      }
    }
  }
}
</script>

<style scoped>
.password-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  width: 80%;
  max-width: 400rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
}

.modal-header {
  padding: 30rpx;
  text-align: center;
  border-bottom: 1rpx solid #e5e5e5;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.modal-body {
  padding: 40rpx 30rpx;
}

.password-label {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 20rpx;
}

.password-input-container {
  margin-bottom: 20rpx;
}

.password-input {
  width: 100%;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.modal-footer {
  display: flex;
  border-top: 1rpx solid #e5e5e5;
}

.cancel-btn,
.confirm-btn {
  flex: 1;
  height: 88rpx;
  line-height: 88rpx;
  border: none;
  font-size: 32rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #666;
}

.confirm-btn {
  background-color: #f03c2e;
  color: #fff;
}
</style>